package com.tianji.learning.mq;

import com.tianji.common.constants.MqConstants;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mq.message.SignInMessage;
import com.tianji.learning.service.IPointsRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author Yandel
 * @date 2025/5/26 下午10:36
 * @description: 学习积分监听器
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class LearningPointsListener {
    private final IPointsRecordService recordService;

    // 监听新增互动问答事件
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "qa.points.queue", durable = "true"),
//            exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
//            key = MqConstants.Key.WRITE_REPLY
//    ))
//    public void listenWriteReplyMessage(Long userId){
//        recordService.addPointsRecord(userId, 5, PointsRecordType.QA);
//    }

    // 监听签到事件
    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name="sign.points.queue", durable="true"),
            exchange = @Exchange(name=MqConstants.Exchange.LEARNING_EXCHANGE,  type = ExchangeTypes.TOPIC),
            key=MqConstants.Key.SIGN_IN
    ))
    //监听到签到消息时，异步添加积分
    public void listenSignInMessage(SignInMessage msg){//message 为发给签到队列的消息
        log.info("签到增加的积分 消费到消息 {}",msg);
        recordService.addPointsRecord(msg,  PointsRecordType.SIGN);
    }

}
